package ccgl.com.server;

import java.sql.SQLException;
import java.util.Map;

import ccgl.com.dao.SpglDao;
import ccgl.com.model.MSp;
import ccgl.com.model.MSpSearchItem;
import ccgl.com.util.UUIDUitl;

/**
 * 货物管理服务接口
 * 
 * @author Administrator
 * 
 */
public class SpglServer {

	SpglDao dao = new SpglDao();

	/**
	 * 添加貨物
	 * @param sp
	 * @return
	 * @throws SQLException 
	 */
	public String addSpxx(MSp sp) throws SQLException {
		sp.setId(UUIDUitl.getNewId());
		if (dao.addSpxx(sp)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	public String updateSpxx(MSp sp) throws SQLException {
		if (dao.updateSpxx(sp)) {
			return "保存成功";
		}
		return "保存出错！";
	}

	public boolean exists(String xh, String userid) throws SQLException {
		return dao.exists(xh, userid);
	}

	public Map getSpList(int star, int end, String userid) throws SQLException {
		return dao.getSpList(star, end, userid);
	}
	
	public Map getSpListWithParams(MSpSearchItem item, int star, int end, String userid) throws SQLException {
		String sql = "SELECT * FROM spb t where userid='"+userid+"'";
		String countSql = "select count(*) from spb t where userid='"+userid+"'";
		
		if(item.getLrsjStar() != null) {
			sql += " and DATEDIFF('" + item.getLrsjStar() + "' , t.lrsj) < 0";
			countSql += " and DATEDIFF('" + item.getLrsjStar() + "' , t.lrsj) < 0";
		}
		
		if(item.getLrsjEnd() != null) {
			sql += " and DATEDIFF('" + item.getLrsjEnd() + "' , t.lrsj) > 0";
			countSql += " and DATEDIFF('" + item.getLrsjEnd() + "' , t.lrsj) > 0";
		}
		
		if(item.getLx() != null) {
			sql += " and t.splx like '%" + item.getLx() + "%'";
			countSql += " and t.splx like '%" + item.getLx() + "%'";
		}
		
		if(item.getSlL() != 0) {
			sql += " and t.sl>" + item.getSlL();
			countSql += " and t.sl>" + item.getSlL();
		}
		
		if(item.getSlT() != 0) {
			sql += " and t.sl<" + item.getSlT();
			countSql += " and t.sl<" + item.getSlT();
		}
		
		if(item.getXh() != null) {
			sql += " and t.xh like '%" + item.getXh() + "%'";
			countSql += " and t.xh like '%" + item.getXh() + "%'";
		}
		
		
		sql +=" LIMIT " + star + "," + end;
		
		return dao.getSpList(sql, countSql);
	}

	public boolean deleteSpxxs(String ids) throws SQLException {
		String[] idArr = ids.split("-");
		String idStr = "";
		for(int i =0;i<idArr.length;i++){
			if(i==(idArr.length-1) ){
				idStr += "'" + idArr[i] + "'";
			}else{
				idStr += "'" + idArr[i] + "',";
			}
		}
		return dao.deleteSpxxs(idStr);
	}
}
